package com.wmyg.antifake.gs1.ns2.converters;

import com.wmyg.antifake.gs1.ns2.*;
import org.dom4j.Element;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: antifake
 * @description: 商品数据记录转换器
 * @author: wong
 * @create: 19-4-23
 */
public class ProductDataRecordConverter {

    protected static LinkedHashMap<String, Model> converter(Element element) {
        LinkedHashMap<String, Model> result = new LinkedHashMap<>();
        List<Element> children = element.elements();
        for (Element e :
                children) {
            singleModelConvert(e, result);
        }
        return result;
    }

    private static void singleModelConvert(Element element, Map result) {
        Model model = null;
        Element child = (Element)element.elements().get(0);
        String type = child.getName();
        switch (type) {
            case "TSDBasicProductInformationModuleType":
                model = BasicProductInformationConverter.convert(child);
                break;
            case "TSDFoodAndBeverageIngredientInformationModuleType":
                model = new TSDFoodAndBeverageIngredientInformationModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDFoodAndBeveragePreparationInformationModuleType":
                model = new TSDFoodAndBeveragePreparationInformationModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDNutritionalProductInformationModuleType":
                model = new TSDNutritionalProductInformationModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDProductAllergenInformationModuleType":
                model = new TSDProductAllergenInformationModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDProductClaimsAndEndorsementsModuleType":
                model = new TSDProductClaimsAndEndorsementsModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDProductInstructionsModuleType":
                model = new TSDProductInstructionsModule();
                model.setMap(MapConverter.elementToMap(child));
                break;
            case "TSDProductOriginInformationModuleType":
                model = ProductOriginInformationConverter.convert(child);
                break;
            case "TSDProductQuantityInformationModuleType":
                model = ProductQuantityInformationConverter.convert(child);
                break;
            default:
        }
        result.put(child.getName(), model);
    }

}
